package cn.edu.zut.internship.service.impl.setting;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.iservice.setting.SettingSV;
import cn.edu.zut.internship.mapper.setting.SettingMapper;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@Service
public class SettingSVImpl implements SettingSV {
    @Autowired
    SettingMapper settingMapper;
    @Override
    public Map getSetting(String key) {
        Map setting = settingMapper.getSetting(key);
        return setting;
    }

    @Override
    public Result getResultSetting() {
        Result result = new Result();
        int peacetimeResult = Integer.parseInt((String)settingMapper.getSetting("peacetimeResult").get("settingValue"));
        int centreResult =Integer.parseInt((String)settingMapper.getSetting("centreResult").get("settingValue"));
        int finalResult = Integer.parseInt((String)settingMapper.getSetting("finalResult").get("settingValue"));
        result.getBean().put("peacetimeResult",peacetimeResult);
        result.getBean().put("centreResult",centreResult);
        result.getBean().put("finalResult",finalResult);
        return result;
    }

    @Override
    public Result editResultSetting(Map params) {
        Result result = new Result();
        Map peacetimeResult = new HashMap();
        peacetimeResult.put("settingKey","peacetimeResult");
        peacetimeResult.put("settingValue",params.get("percentageA"));
        settingMapper.updateSetting(peacetimeResult);
        Map centreResult = new HashMap();
        centreResult.put("settingKey","centreResult");
        centreResult.put("settingValue",params.get("percentageB"));
        settingMapper.updateSetting(centreResult);
        Map finalResult = new HashMap();
        finalResult.put("settingKey","finalResult");
        finalResult.put("settingValue",params.get("percentageC"));
        settingMapper.updateSetting(finalResult);
        return result;
    }

    @Override
    public Result editReportTemplate(Map params) {
        Result result = new Result();
        int reportType = (int) params.get("reportType");
        Map setting = new HashMap();
        if(reportType==0){
            setting.put("settingKey","weekTemplate");
            setting.put("settingValue",params.get("fileUrl"));
        }else if(reportType == 1){
            setting.put("settingKey","monthTemplate");
            setting.put("settingValue",params.get("fileUrl"));
        }else if(reportType == 2){
            setting.put("settingKey","centreTemplate");
            setting.put("settingValue",params.get("fileUrl"));
        }else if(reportType == 3){
            setting.put("settingKey","finalTemplate");
            setting.put("settingValue",params.get("fileUrl"));
        }
        settingMapper.updateSetting(setting);
        return result;
    }

    @Override
    public Result getReportTemplate() {
        Result result = new Result();
        Map weekTemplate = settingMapper.getSetting("weekTemplate");
        Map monthTemplate = settingMapper.getSetting("monthTemplate");
        Map centreTemplate = settingMapper.getSetting("centreTemplate");
        Map finalTemplate = settingMapper.getSetting("finalTemplate");
        result.getBean().put("weekTemplate",weekTemplate.get("settingValue"));
        result.getBean().put("monthTemplate",monthTemplate.get("settingValue"));
        result.getBean().put("centreTemplate",centreTemplate.get("settingValue"));
        result.getBean().put("finalTemplate",finalTemplate.get("settingValue"));
        return result;
    }

    @Override
    public Result editSemesterSetting(Map params) {
        Result result = new Result();
        Map setting = new HashMap();
        setting.put("settingKey","currentSemester");
        setting.put("settingValue",params.get("semester"));
        settingMapper.updateSetting(setting);
        return result;
    }

    @Override
    public Result getSemesterSetting() {
        Result result = new Result();
        Map semester = settingMapper.getSetting("currentSemester");
        result.setBean(semester);
        return result;
    }
}
